using System;
using UnityEngine;

public class Joystick : MonoBehaviour
{
	public static Vector2 joystickAxis = Vector2.zero;

	public static Vector2 gravityAxis = Vector2.zero;

	public static Vector2 joystickend = Vector2.zero;

	public Vector2 gravityend = Vector2.zero;

	private float gravityOffsetX = 0.6f;

	public AnimationCurve gravityCurve;

	public UIEventListener joyStickAreaBtn;

	public UISprite insideCircle;

	public UIWidget outsideCircle;

	public UIWidget allstick;

	public float joyStickLeftBorder;

	public float joyStickBottomBorder;

	public Vector2 defaultCenter;

	public Vector2 center;

	public float sqrRadius;

	public float radius;

	public Vector2 dir;

	public bool isPressed;

	private UICamera.MouseOrTouch currentTouch;

	private Vector2 touchLocalPos;

	private bool isout;

	private float xvale;

	private float yvale;

	private void Awake()
	{
		radius = outsideCircle.width / 2;
		sqrRadius = outsideCircle.width * outsideCircle.width / 4;
		UIEventListener uIEventListener = joyStickAreaBtn;
		uIEventListener.onPress = (UIEventListener.BoolDelegate)Delegate.Combine(uIEventListener.onPress, new UIEventListener.BoolDelegate(OnBtnPress));
		BoxCollider component = base.gameObject.GetComponent<BoxCollider>();
		BoxCollider boxCollider = component;
		Vector3 size = component.size;
		float x = size.x / 2f;
		Vector3 size2 = component.size;
		boxCollider.center = new Vector3(x, size2.y / 2f, 0f);
		Vector3 size3 = component.size;
		float x2 = size3.x / 2f;
		Vector3 size4 = component.size;
		defaultCenter = new Vector2(x2, size4.y / 2f);
		allstick.alpha = 0.3f;
	}

	public void OnBtnPress(GameObject obj, bool isPress)
	{
		if (!isPressed && isPress)
		{
			allstick.alpha = 1f;
			allstick.gameObject.SetActive(true);
		}
		isPressed = isPress;
		if (isPress)
		{
			currentTouch = UICamera.currentTouch;
			touchLocalPos = new Vector2(currentTouch.pos.x / GlobalInf.ratioReltoUI, currentTouch.pos.y / GlobalInf.ratioReltoUI);
			dir = touchLocalPos - center;
			SetCenter(touchLocalPos);
		}
		else
		{
			allstick.alpha = 0.3f;
			currentTouch = null;
			SetCenter(defaultCenter);
			joystickAxis = Vector2.zero;
			joystickend = Vector2.zero;
		}
	}

	private void Update()
	{
		if (GlobalInf.PlaneCtrlType == PLANECTRLTYPE.BUTTON)
		{
			if (isPressed)
			{
				touchLocalPos = new Vector2(currentTouch.pos.x / GlobalInf.ratioReltoUI, currentTouch.pos.y / GlobalInf.ratioReltoUI);
				dir = touchLocalPos - center;
				if (dir.sqrMagnitude > sqrRadius)
				{
					joystickend = dir.normalized;
					SetInsideCircle(center + joystickend * radius);
				}
				else
				{
					SetInsideCircle(touchLocalPos);
					joystickend = dir / radius;
				}
				setjoystick();
				isout = false;
			}
			else if (!isout)
			{
				isout = true;
				allstick.alpha = 0.3f;
				currentTouch = null;
				SetCenter(defaultCenter);
				joystickAxis = Vector2.zero;
				joystickend = Vector2.zero;
			}
		}
		else
		{
			AnimationCurve animationCurve = gravityCurve;
			Vector3 acceleration = Input.acceleration;
			float x = animationCurve.Evaluate((acceleration.y + gravityOffsetX) * 6f);
			AnimationCurve animationCurve2 = gravityCurve;
			Vector3 acceleration2 = Input.acceleration;
			gravityend = new Vector2(x, animationCurve2.Evaluate(acceleration2.x * 4f));
			if (Mathf.Abs(gravityend.y) < 0.1f)
			{
				gravityend.y = 0f;
			}
			gravityAxis = Vector2.Lerp(gravityAxis, gravityend, Time.deltaTime * 10f);
		}
	}

	public void setjoystick()
	{
		xvale = joystickend.x;
		if (GlobalInf.isinverty)
		{
			yvale = joystickend.y;
		}
		else
		{
			yvale = 0f - joystickend.y;
		}
		joystickend = new Vector2(xvale, yvale);
		joystickAxis = Vector2.Lerp(joystickAxis, joystickend, Time.deltaTime * 10f);
	}

	private void OnEnable()
	{
		isPressed = false;
		SetCenter(defaultCenter);
	}

	public void SetInsideCircle(Vector2 pos)
	{
		insideCircle.transform.localPosition = pos;
	}

	public void SetOutsideCircle(Vector2 pos)
	{
		outsideCircle.transform.localPosition = pos;
	}

	public void SetCenter(Vector2 pos)
	{
		center = pos;
		outsideCircle.transform.localPosition = center;
		insideCircle.transform.localPosition = center;
	}
}
